layui.use(['jquery', 'febs', 'table', 'form', 'fileUpload', 'commonJS', 'formSelects', 'validate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        table = layui.table,
        formSelects = layui.formSelects,
        laydate=layui.laydate,
        $view = $('#task-edit'),
        $taskCode = $view.find('#taskCode'),
        $projectCode = $view.find('#projectCode'),
        $paiqi = $view.find('#paiqi'),
        $changshui = $view.find('#changshui'),
        $banliu = $view.find('#banliu'),
        $liuxiang = $view.find('#liuxiang'),
        $status = $view.find('#status'),
        $iskeyTask = $view.find('#iskeyTask'),
        $taskType = $view.find('#taskType'),
        $auditRecord=$view.find('#auditRecord');
        fileUpload = layui.fileUpload,
        commonJS = layui.commonJS,
        validate = layui.validate;
        initfile();
     //   initDictSelect();
        form.verify(validate);
        formSelects.render();
        form.render();
       initFormValue();
    var taskTestData = taskData.taskTestArray || [];
    var testTestcsArray = taskData.taskTestcsArray || [];
    var testTestbl1Array = taskData.taskTestbl1Array || [];
    var testTestbl2Array = taskData.taskTestbl2Array || [];
    var testTestlxArray = taskData.taskTestlxArray || [];
    if (taskData.taskType == 1) {
        initTestTable(taskTestData);
    } else if (taskData.taskType == 2) {
        initTestTablecs(testTestcsArray);
    } else if (taskData.taskType == 3) {
        $paiqi.css('display', 'none');
        $changshui.css('display', 'none');
        $banliu.css('display', 'block');
        $liuxiang.css('display', 'none');
        initTestTablebl1(testTestbl1Array);
        initTestTablebl2(testTestbl2Array);
    } else if (taskData.taskType == 4) {
        initTestTablelx(testTestlxArray);
    }

        initWorkFlowTable();
        renderDate('#planStartdate', 'yyyy-MM-dd');
       renderDate('#planEnddate', 'yyyy-MM-dd');
      //  initMultiSelect(taskData.projectCode,taskData.taskCode);
      var iskey = taskData.iskeyTask;

      $("#fileUploadBtn").css("dispaly","none");
      if(typeData=='home'){
          $("#reset").css("display","none")
      }

     function initWorkFlowTable() {
        tableIns = febs.table.init({
            elem:'#workflowRecordTable',
            data: taskData.workflowArray,
          //  url: ctx + 'workflowRecord/list',
         //   where: getQueryParams(),
            height: getTableHeight() / 5,
            page: false,
            loading: false,
            cols: [[
                {field: 'remark', title: '操作', minWidth: 80},
                {field: 'createdTime', title: '时间', minWidth: 150,templet:function(d){
                            return formatDate(new Date(d.createdTime));
                    }},
            ]]
        });
    }

    function formatDate(date) {
        let y = date.getFullYear()
        let m = date.getMonth() + 1
        let day = date.getDate()
        m = m < 10 ? '0' + m : m
        day = day < 10 ? '0' + day : day
        return y + '-' + m + '-' + day
    }



    // $view.find('#projectlink')不能获取元素
    document.getElementById("projectlink").addEventListener("click", function (event) {
        parent.layui.febs.navigate('/sypqProject/detail/'+ taskData.projectCode,true);
    });


    function initfile() {
        var fileUploadDiv1 = fileUpload.render({
            elem: '#fileUploadBtn',
            url: ctx + 'task/upload',
            type: 2,//0-图片，1-视频，2-其他
            maxNum: 5,
            multiple: true,
            defaultData: taskData && taskData.fileList ? taskData.fileList : [],
            isDetail: true,
            done: function (res) {
            },
            error: function () {
                layer.msg("服务器异常");
            }
        });
    }

    function initTestTable() {
        $paiqi.css('display', 'block');
        $changshui.css('display', 'none');
        $banliu.css('display', 'none');
        $liuxiang.css('display', 'none');
        febs.table.init({
                elem: $("#tasktestTable"),
                data: taskData.taskTestArray,
                page: false,
                loading: false,
                width:936,
                cols: [[
                    {field: 'id', title: 'id', minWidth: 20, hide: true},
                    {field: 'no', title: '序号', type: 'number', minWidth: 10, edit: false, sort: false},
                    {field: 'resistance', title: '阻力', minWidth: 100, edit: false},
                    {field: 'navigate', title: '自航(吃水数量/航速个数,PK试验航速数量,不同时可新增几行1/3、1/2等)', minWidth: 426, edit: false},
                    {field: 'seakeep', title: '耐波', minWidth: 100, edit: false},
                    {field: 'controll', title: '操纵', minWidth: 100, edit: false},
                    {field: 'vain', title: '空泡', minWidth: 100, edit: false},
                ]],
                done: function (res, curr, count) {
                }
            }
        );
    }

    function initTestTablecs(data) {
        $paiqi.css('display', 'none');
        $changshui.css('display', 'block');
        $banliu.css('display', 'none');
        $liuxiang.css('display', 'none');
        tasktableIns = febs.table.init({
            elem: '#tasktestTablecs',
            data: data,
            page: false,
            loading: false,
            even: false,
            height: getTableHeight() / 3,
            width: 790,
            cols: [[
                {field: 'id', title: 'id', minWidth: 20, hide: true},
                {field: 'oarsCode', title: '桨模编号', minWidth: 120, edit: false},
                {field: 'diameter', title: '桨模直径(mm)', minWidth: 100, edit: false},
                {field: 'pitch', title: '螺距比', minWidth: 100, edit: false},
                {field: 'surface', title: '盘面比', minWidth: 100, edit: false},
                {field: 'chord', title: '弦长(mm)', minWidth: 100, edit: false},
                {field: 'leaf', title: '叶厚(mm)', minWidth: 100, edit: false},
                {field: 'remark', title: '备注', minWidth: 120, edit: false},
            ]],
            defaultToolbar: [],
            done: function (res, curr, count) {
            }
        });
        return tasktableIns;
    }

    function initTestTablelx(data) {
        $paiqi.css('display', 'none');
        $changshui.css('display', 'none');
        $banliu.css('display', 'none');
        $liuxiang.css('display', 'block');
        tasktableIns = febs.table.init({
            elem: '#tasktestTablelx',
            data: data,
            page: false,
            loading: false,
            even: false,
            height: getTableHeight() / 3,
            width: 670,
            cols: [[
                {field: 'id', title: 'id', minWidth: 20, hide: true},
                {field: 'shipcode', title: '船模编号', minWidth: 120, edit: false},
                {field: 'draugh', title: '吃水', minWidth: 120, edit: false},
                {field: 'displacement', title: '排水', minWidth: 120, edit: false},
                {field: 'speed', title: '船模速度', minWidth: 120, edit: false},
                {field: 'remark', title: '备注', minWidth: 120, edit: false},
            ]],
            defaultToolbar: [],
            done: function (res, curr, count) {
            }
        });
        return tasktableIns;
    }

    function initTestTablebl1(data) {
        tasktableIns = febs.table.init({
            elem: '#tasktestTablebl1',
            data: data,
            page: false,
            loading: false,
            even: false,
            height: getTableHeight() / 3,
            width: 670,
            cols: [[
                {field: 'id', title: 'id', minWidth: 20, hide: true},
                {field: 'shipCode', title: '船模编号', minWidth: 120, edit: false},
                {field: 'draugh', title: '吃水', minWidth: 120, edit: false},
                {field: 'displacement', title: '排水', minWidth: 120, edit: false},
                {field: 'speed', title: '船模速度', minWidth: 120, edit: false},
                {field: 'remark', title: '备注', minWidth: 120, edit: false},
            ]],
            defaultToolbar: [],
            done: function (res, curr, count) {
            }
        });
        return tasktableIns;
    }


    function initTestTablebl2(data) {
        tasktableIns = febs.table.init({
            elem: '#tasktestTablebl2',
            data: data,
            page: false,
            loading: false,
            even: false,
            height: getTableHeight() / 3,
            width:670,
            cols: [[
                {field: 'id', title: 'id', minWidth: 20, hide: true},
                {field: 'oarsCode', title: '桨模编号', minWidth: 120, edit: false},
                {field: 'diameter', title: '直径mm', minWidth: 120, edit: false},
                {field: 'surface', title: '桨盘面距尾轴出口长度mm', minWidth: 120, edit: false},
                {field: 'direct', title: '桨模旋向', minWidth: 120, edit: false},
                {field: 'remark', title: '备注', minWidth: 120, edit: false},
            ]],
            defaultToolbar: [],
            done: function (res, curr, count) {
            }
        });
        return tasktableIns;
    }

    function getTableHeight() {
    return commonJS.calcTableHeight();
    }

    function renderDate(element, format) {
        laydate.render({
            elem: element,
            type: 'date',
            format: format,
            btns: ['now', 'confirm'],
            trigger: 'click',
            isInitValue: true
        });
    }

    // function initMultiSelect(projectCode,taskCode) {
    //     formSelects.config('modelids', {
    //         searchUrl: ctx + 'task/getModelList',
    //         data: {projectCode: projectCode,taskCode:taskCode},
    //         response: {
    //             statusCode: 200
    //         },
    //         beforeSuccess: function (id, url, searchVal, result) {
    //             var data = result.data;
    //             var tranData = [];
    //             if (data != null) {
    //                 for (var i = 0; i < data.length; i++) {
    //                     tranData.push({
    //                         name: data[i].name,
    //                         value: data[i].code
    //                     })
    //                 }
    //             }
    //             result.data = tranData;
    //             return result;
    //         },
    //         success: function () {
    //             formSelects.value('modelids',
    //                 taskData.modelids == null ? [] : taskData.modelids.split(','));
    //         },
    //         error: function (id, url, searchVal, err) {
    //             console.error(err);
    //             febs.alert.error('获取角色列表失败');
    //         }
    //     });
    // }


// function initDictSelect() {
//   //  febs.initDictItemSelect($taskType, "TASK_TYPE", taskData.taskType, "");
//   //  initResourceOptions($rsid, resourceListData, taskData.rsid, "");
//     if (taskData.createdTime != null) {
//         $taskCode.attr("readonly", true);
//     }
//
// }


// function initProjectOptions(element, data, defaultValue, callBack) {
//     $(element).empty();
//     $(element).append(`<option value="">请选择</option>`);
//     if (data != null && data.length > 0) {
//         data.forEach(item => {
//             if (defaultValue == item.projectCode) {
//                 element.append(`<option value="${item.projectCode}"  selected>${item.projectName}</option>`)
//             } else {
//                 element.append(`<option value="${item.projectCode}" >${item.projectName}</option>`)
//             }
//         })
//     }
//     if (typeof (callBack) === "function") {
//         callBack(result);
//     }
//     layui.form.render('select');
// }

// function initResourceOptions(element, data, defaultValue, callBack) {
//     $(element).empty();
//     $(element).append(`<option value="">请选择</option>`);
//     if (data != null && data.length > 0) {
//
//         data.forEach(item => {
//             if (defaultValue == item.rsid) {
//                 element.append(`<option value="${item.id}"  selected>${item.rsName}</option>`)
//             } else {
//                 element.append(`<option value="${item.id}" >${item.rsName}</option>`)
//             }
//         })
//     }
//     if (typeof (callBack) === "function") {
//         callBack(result);
//     }
//     layui.form.render('select')
// }


function initFormValue() {
    if (taskData) {
        form.val("task-edit-form", {
            "taskCode": taskData.taskCode,
            "projectCode": taskData.projectCode,
            "modelnames": taskData.modelnames,
            "taskType": taskData.taskType,
            "status": taskData.status,
            "testDay": taskData.testDay,
            "iskeyTask": taskData.iskeyTask,
            "keyExplain":taskData.keyExplain,
            "remark": taskData.remark,
            "planStartdate": taskData.planStartdate,
            "planStarttime": taskData.planStarttime,
            "planEnddate": taskData.planEnddate,
            "planEndtime": taskData.planEndtime,
            "actuallyStartdate": taskData.actuallyStartdate,
            "actuallyEnddate": taskData.actuallyEnddate,
            "createdTime": taskData.createdTime,
            "rsid": taskData.rsid,
        });
    }

    $('#reset').click(function () {
        closeCur();
    });

    function closeCur(){
        var currentUrl = window.location.hash.replace('#', '');
        febs.view.tab.del(currentUrl);
        febs.view.tab.change("/task");
        $('#febs-task').find('#query').trigger('click');
    }

    // if (taskData.iskeyTask === '1') {
    //     $iskeyTask.prop("checked", true);
    // } else {
    //     $iskeyTask.prop("checked", false);
    // }
 //   form.render('checkbox');
}


// form.on('checkbox(iskeyTask)', function (data) {
//     if (data.elem.checked) {
//         iskey = '1'
//     } else {
//         iskey = '0'
//     }
// });


    // form.on('submit(task-edit-form-save)', function (data) {
    //     modifySchedule(data.field);
    //     return false;
    // });


    // form.on('submit(task-edit-form-submit)', function (data) {
    //     commitSchedule(data.field);
    //     return false;
    // });



})
;